package com.njupt.huawei;

import java.util.*;

/**
 * @Author: wujiaming
 * @CreateTime: 2025/4/10 13:43
 * @Description:
 * @Version: 1.0
 */


public class Version1_1 {


    public static void main(String[] args) {


        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        sc.nextLine();


        Map<String,String> preVersion = new HashMap<>();
        Set<String> versionSet = new HashSet<>();
        for(int i=0; i < N; i++){
            String[] parts = sc.nextLine().split(" ");
            preVersion.put(parts[0],parts[1]);
            versionSet.add(parts[0]);
            if(!"NA".equals(parts[1])){
                versionSet.add(parts[1]);
            }
        }

        Map<String,Integer> result = new HashMap<>();

        for(String v : versionSet){

            if(result.containsKey(v)){
                continue;
            }
            Deque<String> stack = new LinkedList<>();
            String currV = v;
            while(true){

//                if(result.containsKey(currV)){
//                    break;
//                }

                String pre = preVersion.get(currV);
                stack.push(currV);
                if(!"NA".equals(pre)){
                    currV = pre;
                }else {
                    break;
                }
            }

            int baseDepth = 0;
//            if(result.containsKey(currV)){
//                baseDepth = result.get(currV);
//            }else {
//                baseDepth = 0;
//            }
            while(!stack.isEmpty()){
                String node = stack.pop();

                String pre = preVersion.get(node);
                if(!"NA".equals(pre)){
                    baseDepth += 1;
                }else {
                    baseDepth = 0;
                }
                result.put(node,baseDepth);
            }
        }

        int maxDepth = -1;
        List<String> re = new ArrayList();
        for(String v : versionSet){
            Integer currentDepth = result.get(v);
            if(currentDepth > maxDepth){
                maxDepth = currentDepth;
                re.clear();
                re.add(v);
            }else if(currentDepth == maxDepth){
                re.add(v);
            }
        }
        Collections.sort(re);
        System.out.println(String.join(" ",re));

    }
}
